package com.kingdee.shr.recuritment.web.handler.recRegister;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemCollection;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.handler.SingleRowHandler;
import com.kingdee.shr.preentry.PreEntryPersonInfo;
import com.kingdee.shr.recuritment.IPreEntryPersonEx;
import com.kingdee.shr.recuritment.PreEntryPersonExCollection;
import com.kingdee.shr.recuritment.PreEntryPersonExFactory;
import com.kingdee.shr.recuritment.PreEntryPersonExInfo;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;

public class RecRegisterResumeBaseHandler extends SingleRowHandler {
    protected CoreBaseInfo getValue(HttpServletRequest request, String modelID) throws SHRWebException {
        Context ctx = SHRContext.getInstance().getContext();
        PreEntryPersonInfo resumeInfo = (PreEntryPersonInfo) super.getValue(request, modelID);
        if ((resumeInfo != null) && (resumeInfo.getId() != null)) {
            setPersonAge(ctx, resumeInfo);

            setExtendData(ctx, resumeInfo);
        }

        return resumeInfo;
    }

    private void setPersonAge(Context ctx, PreEntryPersonInfo resumeInfo) {
        if (resumeInfo.getBirthday() != null) {
            Calendar birthday = Calendar.getInstance();
            birthday.setTime(resumeInfo.getBirthday());
            Calendar now = Calendar.getInstance();
            int age = now.get(1) - birthday.get(1);
            now.set(1, birthday.get(1));
            if (birthday.after(now)) {
                age -= 1;
            }
            resumeInfo.put("age", Integer.valueOf(age));
        }
    }

    private void setExtendData(Context ctx, PreEntryPersonInfo resumeInfo) {
        PreEntryPersonExInfo exInfo = null;
        try {
            EntityViewInfo view = new EntityViewInfo();
            FilterInfo filter = new FilterInfo();
            filter.getFilterItems().add(
                    new FilterItemInfo("talent", resumeInfo.getId().toString(), CompareType.EQUALS));
            view.setFilter(filter);
            SelectorItemCollection selector = new SelectorItemCollection();
            selector.add("*");
            selector.add("resSource.name");
            selector.add("enterpriseNature.name");
            selector.add("ccyx.name");
            view.setSelector(selector);
            PreEntryPersonExCollection coll = PreEntryPersonExFactory.getLocalInstance(ctx)
                    .getPreEntryPersonExCollection(view);
            if ((coll != null) && (coll.size() > 0))
                exInfo = coll.get(0);
        } catch (BOSException e) {
            e.printStackTrace();
        }

        resumeInfo.put("extend", exInfo);
    }
}
